package com.chengjue.service.impl;

import com.chengjue.service.UserService;
import com.chengjue.common.Const;
import com.chengjue.common.ServerResponse;
import com.chengjue.dao.UserModelMapper;
import com.chengjue.model.UserModel;
import com.chengjue.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserModelMapper userModelMapper;

    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public ServerResponse<String> register(UserModel user) {
        logger.info("register");
        //检查手机号是否已经注册
        System.out.println(user);
        ServerResponse validResponse = checkVolid(user.getShouji(), Const.PHONE);
        if (!validResponse.isSuccess()) {
            return validResponse;
        }
        //数据库插入数据
        user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        int result = userModelMapper.insert(user);
        if (result == 0) {
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccessMessage("注册成功");
    }

    @Override
    public ServerResponse<UserModel> login(UserModel user) {
        int resultCount = userModelMapper.ceckUserName(user.getUsername());
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("用户不存在");
        }

        String passWord = MD5Util.MD5EncodeUtf8(user.getPassword());
        UserModel userModel = userModelMapper.selectLogin(user.getUsername(), passWord);
        if(userModel == null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        userModel.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登录成功",userModel);
    }

    @Override
    public ServerResponse<String> resetPassWord(Long id, String old, String newPassword) {
        logger.info("userID:"+id+"oldPassWord:"+MD5Util.MD5EncodeUtf8(old));

        int resultCount = userModelMapper.checkPassWord(id,MD5Util.MD5EncodeUtf8(old));
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("用户名密码错误");
        }
       int updateCount = userModelMapper.updatePassWord(id,MD5Util.MD5EncodeUtf8(newPassword));
        if(updateCount > 0){
            return ServerResponse.createBySuccessMessage("密码更新成功");
        }

        return  ServerResponse.createByErrorMessage("密码更新失败");
    }

    private ServerResponse checkVolid(String data, String type) {

        if (StringUtils.isNotBlank(data)) {
            int resultCount = 0;
            switch (type) {
                case Const.PHONE:
                    resultCount = userModelMapper.checkPhone(data);
                    if (resultCount > 0) {
                        return ServerResponse.createByErrorMessage("手机号已经注册");
                    }
                    break;

                default:
                    break;
            }

        } else {
            return ServerResponse.createByErrorMessage("参数错误");
        }

        return ServerResponse.createBySuccessMessage("参数检验成功");


    }


}
